package bar.dark.lee.fan;

import java.math.BigDecimal;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class FanUpdateListActivity extends SuperActivity
{
	public static final String FAN_UPDATE_FLAG = "fan_update";
	public static final String FAN_UPDATE_DATA = "fan_update_data";
	
	public static final int IN = 1;
	public static final int OUT = 2;
	
	private ListView listView;
	
	private ArrayList<FanData> list;
	
	private int inoutFlag;
	
	private DBSQLite sql;
	
	ArrayList<FanData> fanList = new ArrayList<FanData>();
	
	private View nodataView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setFindView();
		
		setGoneTipButton();
	}

	private void setFindView()
	{
		sql = DBSQLite.getInstance(this);
		
		setContentView(R.layout.fan_update_list_activity);
		
		listView = (ListView) findViewById(R.id.fanUpdateListview);
		
		nodataView = getLayoutInflater().inflate(R.layout.view_no_result, null);
	}
	
	private void setData()
	{
		Intent intent = getIntent();
		
		inoutFlag = intent.getIntExtra(FAN_UPDATE_FLAG, -1);
		list = intent.getParcelableArrayListExtra(FAN_UPDATE_DATA);
	}

	private void setStatsData(int inoutFlag)
	{
		String categoryName = "";
		if (inoutFlag == IN)
		{
			setTitle("받은돈 리스트");
			categoryName = BDConstents.CATEGORY_FAN_OUT_CHECK;
		}
		else if (inoutFlag == OUT)
		{
			setTitle("갚은돈 리스트");
			categoryName = BDConstents.CATEGORY_FAN_IN_CHECK;
		}
		
		fanList.clear();
		
		for (int i=0; i < list.size(); i++)
		{
			sql.selectMapTile(this, "SELECT * FROM in_out WHERE id = "
					+ list.get(i).getLinkId() + " and category = '" 
					+ categoryName + "';", new ParseSelectData() {
				@Override
				public void resultData(Cursor c) {
					int id = c.getColumnIndex("id");
					int date = c.getColumnIndex("date");
					int linktype = c.getColumnIndex("linktype");
					int outmoney = c.getColumnIndex("outmoney");
					int postmoney = c.getColumnIndex("postmoney");
					int inmoney = c.getColumnIndex("inmoney");
					int moneykind = c.getColumnIndex("moneykind");
					int memo = c.getColumnIndex("memo");
					
					c.moveToFirst();
					
					for (int i = 0; i < c.getCount(); i++)
					{
						FanData data = new FanData();
						
						data.setId(list.get(i).getId());
						data.setCheckSign(list.get(i).getCheckSign());
						data.setLinkId(c.getInt(id)+"");
						data.setDate(c.getString(date));
						if (c.getString(linktype).equals(BDConstents.BANK_TYPE))
						{
							data.setType(BDConstents.BANK_TYPE);
							data.setMoneyname(c.getString(moneykind));
						}
						else if (c.getString(linktype).equals(BDConstents.CHECK_TYPE))
						{
							data.setType(BDConstents.CHECK_TYPE);
							data.setMoneyname(c.getString(moneykind));
						}
						else if (c.getString(linktype).equals(BDConstents.CREDIT_TYPE))
						{
							data.setType(BDConstents.CREDIT_TYPE);
							data.setMoneyname(c.getString(moneykind));
						} 
						else
						{
							data.setType(BDConstents.REAL_TYPE);
						}
						
						if (new BigDecimal(c.getString(inmoney)).compareTo(BigDecimal.ZERO) == 1)
						{
							data.setMoney(c.getString(inmoney));
						}
						else if (new BigDecimal(c.getString(outmoney)).compareTo(BigDecimal.ZERO) == 1)
						{
							data.setMoney(c.getString(outmoney));
						}
						else if (new BigDecimal(c.getString(postmoney)).compareTo(BigDecimal.ZERO) == 1)
						{
							data.setMoney(c.getString(postmoney));
						}
						
						data.setMemo(c.getString(memo));
						//data.setCheckSign("-1");
						
						fanList.add(data);
						
						c.moveToNext();
					}
				}
			});
		}
		
		if (listView.getFooterViewsCount() > 0)
		{
			listView.removeFooterView(nodataView);
		}
		
		if (fanList.size() <= 0)
		{
			listView.addFooterView(nodataView, null, false);
			listView.setFooterDividersEnabled(false);
		}
		
		FanUpdateAdapter adapter = new FanUpdateAdapter(this, R.layout.list_item_fan_update, fanList , inoutFlag);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(adapter);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		new ShowDialog().execute(0);
		
	}
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(FanUpdateListActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0)
			{
				setData();
				return 0;
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				setStatsData(inoutFlag);
			}
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_STATS);
		startActivity(intent);
	}
}
